package com.wantong.jianpu.activitys;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

import android.os.Bundle;

import com.lxj.xpopup.XPopup;
import com.wantong.jianpu.BR;
import com.wantong.jianpu.R;
import com.wantong.jianpu.base.BaseXActivity;
import com.wantong.jianpu.databinding.ActivityFaultRepairBinding;
import com.wantong.jianpu.event.FaultListEvent;
import com.wantong.jianpu.viewmodels.FaultRepairViewModel;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

/**
 * @author PACY
 * @date 2022/3/23
 * description: 故障报修
 */
public class FaultRepairActivity extends BaseXActivity<ActivityFaultRepairBinding, FaultRepairViewModel> {

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_fault_repair;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void initData() {
        super.initData();
        setToolbar(binding.baseToolBar);
        setStateLayout(binding.state);
        EventBus.getDefault().register(this);
        binding.swipeRefresh.setOnRefreshListener(() -> {
            viewModel.loadData(true);
        });

        viewModel.refreshEvent.observe(this, aBoolean -> {
            binding.swipeRefresh.setRefreshing(aBoolean);
        });
        viewModel.rankDialogEvent.observe(this, strings -> {
            new XPopup.Builder(FaultRepairActivity.this)
                    .asBottomList("请选择维修等级", strings,
                            (position, text) -> {
                                viewModel.setRankPosition(position);
                            }).show();
        });

        viewModel.userDialogEvent.observe(this, strings -> {
            new XPopup.Builder(FaultRepairActivity.this)
                    .asBottomList("请选择维修人员", strings,
                            (position, text) -> {
                                viewModel.setUserPosition(position);
                            }).show();
        });

        viewModel.stateDialogEvent.observe(this, strings -> {
            new XPopup.Builder(FaultRepairActivity.this)
                    .asBottomList("请选择维修状态", strings,
                            (position, text) -> {
                                viewModel.setStatePosition(position);
                            }).show();
        });
    }

    @Subscribe
    public void onEvent(FaultListEvent event){
        viewModel.loadData(true);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(EventBus.getDefault().isRegistered(this)){
            EventBus.getDefault().unregister(this);
        }
    }
}